package com.tianheng.modbus.entity;


import com.fasterxml.jackson.annotation.JsonIgnore;
import com.tianheng.modbus.enums.FunctionCodeType;
import lombok.Data;
import lombok.SneakyThrows;
import lombok.ToString;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;


import javax.persistence.*;


@Data
@Entity

public class ModbusDeviceTemplateProperty {

	@Id
	private Long id;

	@ToString.Exclude
	@JsonIgnore
	@ManyToOne
	@NotFound(action= NotFoundAction.IGNORE)
	@JoinColumn(name="modbus_device_template_id", referencedColumnName = "id")
	private ModbusDeviceTemplate modbusDeviceTemplate;

	private String name;

	private String code;

	@Column(name="function_code")
	private String functionCodeOriginal;

	@Column(name = "start_address")
	private Integer startAddress;

	@Column(name = "segment_qty")
	private Integer segmentQty;

	@Column(name = "zoom_expression")
	private String zoomExpression;


	@Column(name = "value_is_true_label")
	private String valueIsTrueLabel;

	@Column(name = "value_is_false_label")
	private String valueIsFalseLabel;

	private Double max;

	private Double min;

	private Long size;

	@Column(name = "is_writeable")
	private Boolean isWriteable;



	@Transient
	private FunctionCodeType functionCode;



	@SneakyThrows
	// 枚举项不能为空
	public FunctionCodeType getFunctionCode() {

		// 用于Data JPA -> Service
		if (functionCodeOriginal != null) {
			return FunctionCodeType.getByCode(this.functionCodeOriginal);
		}
		// 用于Restful -> Service
		if (functionCode != null) {
			return functionCode;
		}
		return FunctionCodeType.getByCode("01");

	}



}